package cn.bird.db.page;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.Data;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Data
public class PageBase {
    /**
     * 页码 从1开始
     */
    private Long pageNum;
    /**
     * 每页条数
     */
    private Long pageSize;
    /**
     * 倒序排序字段
     */
    private List<String> descs;
    /**
     * 正序排序字段
     */
    private List<String> ascs;

    public <T> Page<T> getPage(){
        Page<T> page = new Page<>();
        if (CollectionUtils.isNotEmpty(ascs)) {
            page.addOrder(ascs.stream().map(OrderItem::asc).collect(Collectors.toList()));
        }
        if (CollectionUtils.isNotEmpty(descs)) {
            page.addOrder(descs.stream().map(OrderItem::asc).collect(Collectors.toList()));
        }
        if (Objects.isNull(pageSize) || pageSize < 1) {
            page.setSize(300);
        } else {
            page.setSize(pageSize);
        }
        if (Objects.isNull(pageNum)) {
            pageNum = 1L;
        }
        page.setCurrent(pageNum);
        return page;
    }

}
